﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace StoveTop
{
	// Fragment logic for timer
	public class TimerFragment : Fragment
	{
		private static string MAX_TIME = "max";
		private static string ELAPSED_TIME = "elapsed";
		private static string RUNNING = "running";

		private Handler timerHandler;
		private TimerView timerView;
		private double maxTime;
		private double elapsedTime;
		private bool isRunning;

		public static TimerFragment CreateTimer(double seconds)
		{
			TimerFragment fragment = new TimerFragment ();
			fragment.Arguments = new Bundle ();
			fragment.Arguments.PutDouble (MAX_TIME, seconds);
			fragment.Arguments.PutDouble (ELAPSED_TIME, 0);
			fragment.Arguments.PutBoolean (RUNNING, false);

			return fragment;
		}

		// If arguments were passed to the TimerFragment, initialize from those
		// If a saved state was passed in, initialize from that instead of arguments
		public override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			this.timerHandler = new Handler ();

			if (savedInstanceState != null)
			{
				this.maxTime = savedInstanceState.GetDouble (MAX_TIME);
				this.elapsedTime = savedInstanceState.GetDouble (ELAPSED_TIME);
				this.isRunning = savedInstanceState.GetBoolean (RUNNING);
			}
			else if (this.Arguments != null)
			{
				this.maxTime = this.Arguments.GetDouble (MAX_TIME);
				this.elapsedTime = this.Arguments.GetDouble (ELAPSED_TIME);
				this.isRunning =  this.Arguments.GetBoolean (RUNNING);
			}

			if (this.isRunning)
			{
				this.timerHandler.PostDelayed (this.Tick, 1000);
			}
		}

		public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			this.timerView = new TimerView (this.Activity);
			this.timerView.SetColor(TimerView.Color.Green);

			this.timerView.SetVisibleAngle (this.GetTimeAngle());

			if (this.maxTime != 0)
			{
				this.timerView.Click += this.TimerClick;
				this.timerView.SetText (this.GetTimeString ());
			}
			else
			{
				this.timerView.SetText ("NO TIMER");
			}

			return this.timerView;
		}

		public override void OnSaveInstanceState (Bundle outState)
		{
			base.OnSaveInstanceState (outState);
			outState.PutDouble (MAX_TIME, this.maxTime);
			outState.PutDouble (ELAPSED_TIME, this.elapsedTime);
			outState.PutBoolean (RUNNING, this.isRunning);
			Console.WriteLine ("INSTANCE FRAGMENT SAVED");
		}

		private string GetTimeString()
		{
			return (this.maxTime - this.elapsedTime).ToString ("N0");
		}

		private int GetTimeAngle()
		{
			return (int)(360 * (1 - this.elapsedTime / this.maxTime));
		}

		private void TimerClick(object sender, EventArgs e)
		{
			if (!this.isRunning && this.elapsedTime == 0)
			{
				this.isRunning = true;
				this.timerHandler.PostDelayed (this.Tick, 1000);
				this.timerView.SetColor (TimerView.Color.Green);
			}
			else if (!this.isRunning && this.maxTime != this.elapsedTime)
			{
				this.isRunning = true;
				this.timerView.SetColor (TimerView.Color.Green);
			}
			else if(this.isRunning)
			{
				this.isRunning = false;
				this.timerView.SetColor (TimerView.Color.Yellow);
			}
		}

		private void Tick()
		{
			if(this.isRunning)
			{
				this.elapsedTime++;

				this.timerView.SetVisibleAngle(this.GetTimeAngle());
				this.timerView.SetText (this.GetTimeString());
			}

			if (this.elapsedTime == this.maxTime)
			{
				this.isRunning = false;
				this.timerView.SetColor (TimerView.Color.Red);
			}
			else
			{
				this.timerHandler.PostDelayed (this.Tick, 1000);
			}
		}
	}
}

